<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()
const router = useRouter()
const username = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const loading = ref(false)

const register = async () => {
  if (password.value !== confirmPassword.value) {
    alert('两次输入的密码不一致')
    return
  }

  if (!username.value || !email.value || !password.value) {
    alert('请填写所有必填字段')
    return
  }

  loading.value = true
  try {
    const result = await userStore.register({
      username: username.value,
      email: email.value,
      password: password.value,
    })

    if (result.success) {
      alert('注册成功，请登录')
      router.push('/login')
    } else {
      alert(result.message || '注册失败')
    }
  } catch (error) {
    alert('注册过程中发生错误: ' + error.message)
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-form">
      <h2>用户注册</h2>
      <div class="form-group">
        <label for="username">用户名:</label>
        <input
          id="username"
          v-model="username"
          type="text"
          placeholder="请输入用户名"
          class="form-input"
        />
      </div>
      <div class="form-group">
        <label for="email">邮箱:</label>
        <input
          id="email"
          v-model="email"
          type="email"
          placeholder="请输入邮箱"
          class="form-input"
        />
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input
          id="password"
          v-model="password"
          type="password"
          placeholder="请输入密码"
          class="form-input"
        />
      </div>
      <div class="form-group">
        <label for="confirmPassword">确认密码:</label>
        <input
          id="confirmPassword"
          v-model="confirmPassword"
          type="password"
          placeholder="请再次输入密码"
          class="form-input"
        />
      </div>
      <button @click="register" :disabled="loading" class="register-button">
        {{ loading ? '注册中...' : '注册' }}
      </button>
      <div class="login-link">已有账号？<a @click="goToLogin">立即登录</a></div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.register-form {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 350px;
}

h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #333;
}

.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #409eff;
}

.register-button {
  width: 100%;
  padding: 0.75rem;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 1rem;
}

.register-button:hover:not(:disabled) {
  background-color: #337ecc;
}

.register-button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.login-link {
  text-align: center;
  margin-top: 1rem;
  color: #666;
}

.login-link a {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
</style>
